/**
 * http://brucexx.iteye.com/
 */
package com.brucexx.wedding.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;

/**
 * @author BruceXX email:brucest0078@gmail.com
 * 
 */
public class BeanHelper {

	public static <T> List<String> getBeanProperites(Class<T> clz) {
		List<String> list = new ArrayList<String>();
		Field[] fields = clz.getDeclaredFields();
		for (Field field : fields) {
			list.add(field.getName());
		}
		return list;
	}

	public static void copyProperties2JSON(JSONObject json, Object bean) {
		List<String> propsName = getBeanProperites(bean.getClass());
		for (String prop : propsName) {
			try {
				json.put(prop, BeanUtils.getProperty(bean, prop));
			} catch (Exception e) {
				continue;
			}
		}
	}

	public static void copyProperties2JSON(JSONObject json, Object bean,
			String... externProps) {
		List<String> propsName = getBeanProperites(bean.getClass());
		for (String prop : propsName) {
			boolean t = false;
			for (String extProp : externProps) {
				if (prop.equals(extProp)) {
					t = true;
					break;
				}
			}
			if (t)
				continue;
			try {
				json.put(prop, BeanUtils.getProperty(bean, prop));
			} catch (Exception e) {
				continue;
			}
		}
	}

}
